package com.ruoyi.app.controller.region;

import com.ruoyi.app.business.regiion.RegionBusiness;
import com.ruoyi.app.domain.region.RegionResult;
import com.ruoyi.common.core.utils.CommonUtils;
import com.ruoyi.common.core.web.domain.APIResult;
import com.ruoyi.common.core.web.page.CommonPageInfo;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.service.region.model.RegionModel;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * 地区选择器
 *
 * @author Zane
 * @since 2025-09-27
 */
@RestController
@RequestMapping("/region/")
public class RegionController {

  @Resource
  private RegionBusiness regionBusiness;

  /**
   * 获取省
   *
   * @return 省列表
   */
  @GetMapping("province")
  public TableDataInfo<RegionModel> getProvince(
      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
    CommonPageInfo<RegionModel> list = regionBusiness.getProvince();
    return CommonUtils.buildTableData(list);
  }

  /**
   * 获取市
   *
   * @param provinceId 省ID
   * @param pageNum    页码
   * @param pageSize   页大小
   * @return 市列表
   */
  @GetMapping("city")
  public TableDataInfo<RegionModel> getCity(
      @RequestParam(value = "provinceId") Integer provinceId,
      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
    CommonPageInfo<RegionModel> list = regionBusiness.getCity(provinceId);
    return CommonUtils.buildTableData(list);
  }

  /**
   * 获取县
   *
   * @param cityId   市ID
   * @param pageNum  页码
   * @param pageSize 页大小
   * @return 县列表
   */
  @GetMapping("county")
  public TableDataInfo<RegionModel> getCounty(
      @RequestParam(value = "cityId") Integer cityId,
      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
    CommonPageInfo<RegionModel> list = regionBusiness.getCounty(cityId);
    return CommonUtils.buildTableData(list);
  }

  /**
   * 获取市(首字母排序)
   */
  @GetMapping("citySplit")
  public APIResult<List<RegionResult>> getCitySplit() {
    List<RegionResult> list = regionBusiness.getCitySplit();
    return APIResult.success(list);
  }
}
